<?php
require_once("../DAO/UserDAO.php");
include_once("../Utility/WebUtilities.php");
include_once("../DAO/TestDAO.php");
include_once("../Utility/WebSession.php");

if(WebSession::CurrentUserNoReturn() == null || WebSession::CurrentUserNoReturn()->Role != WebUtilities::GetAdminRoleId())
{
	WebUtilities::Redirect("index.php");
}

$testDAO = new TestDAO();
$comand = WebUtilities::GetQueryString("hdCommand", "");
if($comand == 1) // delete
{
	$ids = split(",", WebUtilities::GetQueryString("hdSelected", ""));
	for($i = 0; $i < count($ids); $i++)
	{
		$testDAO->DeleteTest($ids[$i]);
	}
}

$pageSize = WebUtilities::GetPageSize();
$page = WebUtilities::GetQueryString("page", 1);
$totalPage = 0;

$testList = $testDAO->GetTests($page, $pageSize, $totalPage);
$testStr = "";
if(count($testList) == 0)
{
	$testStr = "<tr><td colspan='6'>No items</td></tr>";
}
else
{
	$userDAO = new UserDAO();
	for($i = 0; $i < count($testList); $i++)
	{
		$item = $testList[$i];
		$user = $userDAO->GetUserById($item->CreatedBy);
		$testStr = $testStr . "<tr><td class='action'><input type='checkbox' onclick='checkProcess.checkItem(this.checked)' class='checkItems' name='checkItems" . $i . "' value='" . $item->Id . "'/></td>"
			. "<td>" . $item->Id . "</td>"
			. "<td>" . $item->Name . "</td>"
			. "<td><a onclick=\"ShowDescription('" . $item->Description . "')\" >View Description</a></td>"
			. "<td>" . ($user == null ? "" : $user->Username) . "</td>"
			. "<td>" . $item->CreationOn . "</td></tr>";
	}
}

$submitUrl = "TestManagement.php?page=" . $page;
$pageUrlFirst = "<a href='TestManagement.php?page=1'>First</a>";
$pageUrlLast = "<a href='TestManagement.php?page=" . $totalPage ."'>Last</a>";
$pageUrlNext = "<a href='TestManagement.php?page=" . ($page + 1) ."'>Next ></a>";
$pageUrlPrev = "<a href='TestManagement.php?page=" . ($page - 1) ."'>< Prev</a>";
if($page <= 1)
{
	$pageUrlFirst = "First";
	$pageUrlPrev = "< Prev";
}
if($page >= $totalPage)
{
	$pageUrlLast = "Last";
	$pageUrlNext = "Next >";
}
$mainContent = "<form enctype='multipart/form-data' action='" . $submitUrl ."' method='POST'>
					<table class='manager' id='tableAnswers'>
						<thead>
							<tr>
								<th><input type='checkbox' id='checkAllItem' name='checkAllItem' onclick='checkProcess.checkAll(this.checked)' /></th>
								<th>Test Id</th>
								<th>Name</th>
								<th>Description</th>
								<th>Created By</th>
								<th>Creation On</th>
							</tr>
						</thead>
						<tbody class='tableBody'>"
							. $testStr . 
						"</tbody>
					</table>" .
				"<div class='paging'>
					Page " . ($page >= $totalPage ? $totalPage : $page) . " of " . $totalPage . " " . 
					$pageUrlFirst . " | " . $pageUrlPrev . " | " . $pageUrlNext . " | " . $pageUrlLast . 
				"</div><div class='paging'>
					<div>
						<input type='button' value='Delete' name='btDelete' onclick=\"DeleteItems('tests')\"/>
						<input type='button' value='Add' name='btAdd' onclick=\"window.location='EditTest.php?page=" . $page . "';\"/>
					</div>
				</div>
				<input type='hidden' id='hdSelected' name='hdSelected' value='' />
				<input type='hidden' id='hdCommand' name='hdCommand' value='' />
				</form>
				<div id='popupPanel' class='popupPanel'>
					<a href='#' onclick='ClosePopupPanel(); return false;'>Close</a>
					<div id='panelContent' style='padding:10px 10px 10px 10px;overflow:auto;'></div>
				</div>";
$scriptContent = "checkProcess.totalItems = " . count($testList) . ";" . 
	"checkProcess.checkAllItems = document.getElementById('checkAllItem');";
$pageIndex = 3;
include("MasterPage.php");
?>